package com.caifeng.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class VgroupchatAdapter extends BaseExpandableListAdapter {

    private List<VgroupchatGroupBean> mList;
    private LayoutInflater inflater;

    public VgroupchatAdapter(List<VgroupchatGroupBean> mList, Context context) {
        this.mList = mList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).getmList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return  mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).getmList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    /**
     *
     */
    public class ViewHolderGroup {
        private ImageView imageViewIconIndicator;
        private TextView chat_title, chat_num;
    }

    public class ViewHolderChild {
        private ImageView chat_ch_imag;
        private TextView chat_name;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup viewHolderGroup = null;
        if (convertView == null) {
            viewHolderGroup = new ViewHolderGroup();
            convertView = inflater.inflate(R.layout.two_groupchat_group, parent, false);
            viewHolderGroup.imageViewIconIndicator = convertView.findViewById(R.id.chat_imag);
            viewHolderGroup.chat_title = convertView.findViewById(R.id.chat_title);
            viewHolderGroup.chat_num = convertView.findViewById(R.id.chat_num);
            convertView.setTag(viewHolderGroup);

        } else {
            viewHolderGroup = (ViewHolderGroup) convertView.getTag();
        }
        VgroupchatGroupBean vgroupchatGroupBean = mList.get(groupPosition);
        if (isExpanded) {
            viewHolderGroup.imageViewIconIndicator.setImageResource(R.mipmap.dnow);

        } else {
//            viewHolderGroup.gr_name.setText(tencentGroupBean.getGr_title());
            viewHolderGroup.imageViewIconIndicator.setImageResource(R.mipmap.you);
        }
        viewHolderGroup.chat_title.setText(vgroupchatGroupBean.getChat_title());
        viewHolderGroup.chat_num.setText(vgroupchatGroupBean.getChat_num());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderChild viewHolderChild = null;
        if (convertView == null) {
            viewHolderChild = new ViewHolderChild();
            convertView=inflater.inflate(R.layout.two_groupchat_child,parent,false);
            VgroupchatGroupBean.VgroupchatChildBean vgroupchatChildBean =
                    mList.get(groupPosition).getmList().get(childPosition);

            viewHolderChild.chat_ch_imag = convertView.findViewById(R.id.chat_ch_imag);
            viewHolderChild.chat_name = convertView.findViewById(R.id.chat_name);
            convertView.setTag(viewHolderChild);

        } else {
            viewHolderChild = (ViewHolderChild) convertView.getTag();
        }

        VgroupchatGroupBean.VgroupchatChildBean vgroupchatChildBean =
                mList.get(groupPosition).getmList().get(childPosition);

        viewHolderChild.chat_ch_imag.setImageResource(vgroupchatChildBean.getChat_ch_imag());
        viewHolderChild.chat_name.setText(vgroupchatChildBean.getChat_name());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
